var app = getApp();
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0,
    curShow: 'good',
    isShowcoupon: false
  },
  onLoad: function (e) {
    var that = this;
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    if (!app.globalData.plumSession) {
      console.log("暂未获取到session");
      app.wechatSq(that);
    } else {
      that.requestShopinfo();
      that.requestGoodslist();
      app.requestAd(that)
    }
  },
  onShow: function () {
    app.setNavtitle('积分商城');
    var that = this;
    app.setCartnum();//更新购物车数量
  },
  toggleShow: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      showLoading: true,
      noMoretip: false,
      page: 0,
      goodsList: null,
      curShow: type
    })
    if (type == 'good') {
      that.requestGoodslist();
    } else if (type == 'coupon') {
      that.requestCouponlist();
    }
  },
  showCoupon: function (e) {
    var that = this;
    var isexchange = e.currentTarget.dataset.isexchange;
    var remaincount = e.currentTarget.dataset.remaincount;
    if (remaincount > 0 && isexchange == 1) {
      var coupon = e.currentTarget.dataset.coupon;
      var duihuanIndex = e.currentTarget.dataset.index;
      that.setData({
        isShowcoupon: true,
        couponInfo: coupon,
        duihuanIndex: duihuanIndex
      })
      console.log(that.data.couponInfo)
    }
  },
  hideCoupon: function () {
    var that = this;
    that.setData({
      isShowcoupon: false
    })
  },
  couponGood: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    var id = e.currentTarget.dataset.couponid;
    if (type == 1) {
      wx.navigateTo({
        url: '/subpages1/allgoodsPage/allgoodsPage',
      })
    } else if (type == 2) {
      wx.navigateTo({
        url: '/subpages/couponGood/couponGood?title=适用商品&id=' + id,
      })
    }
  },
  requestShopinfo: function () {
    var that = this;
    var data = {};
    data.map = 'applet_community_points_shop';
    //发起请求，获取列表列表
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var shopInfo = res.data.data;
          that.setData({
            shopInfo: shopInfo
          })
          if (shopInfo.goodsNum <= 0 && shopInfo.couponNum > 0) {
            that.setData({
              curShow: 'coupon'
            })
            that.requestCouponlist();
          }
          wx.setStorage({
            key: 'jfrules',
            data: res.data.data.rule,
            success: function () {
              console.log("积分规则存储成功")
            }
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
      }
    });
  },
  requestSignin: function () {
    var that = this;
    var data = {};
    data.map = 'applet_community_attendance';
    wx.showLoading({
      title: '加载中',
    })
    //发起请求，获取列表列表
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var shopInfo = that.data.shopInfo;
          shopInfo.hadAttendance = 1;
          shopInfo.points = parseInt(res.data.data.point) + parseInt(shopInfo.points);
          that.setData({
            shopInfo: shopInfo
          })
          var msg = '+' + res.data.data.point + ' ' + res.data.data.msg;
          app.errorTip(that, msg, 2000);
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  requestGoodslist: function () {
    var that = this;
    var data = {};
    var page = that.data.page;
    data.map = 'applet_community_points_goods';
    data.page = page;
    if (that.data.type) {
      data.type = that.data.type;
    }
    if (that.data.sid) {
      data.sid = that.data.sid;
    }
    if (that.data.flid) {
      data.flid = that.data.flid;
    }
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.goodsList ? that.data.goodsList : '[]';
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          page++;
          that.setData({
            goodsList: allArr,
            page: page
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.goodsList);
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              goodsList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  requestCouponlist: function () {
    var that = this;
    var data = {};
    var page = that.data.page;
    data.map = 'applet_community_points_couponList';
    data.page = page;
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.goodsList ? that.data.goodsList : '[]';
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          page++;
          that.setData({
            goodsList: allArr,
            page: page
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.goodsList);
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              goodsList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  confirmDuihuan: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认兑换该优惠券吗？',
      success: function (res) {
        if (res.confirm) {
          that.requestChangecoupon();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 请求会员卡详情
  requestChangecoupon: function () {
    var that = this;
    var goodsList = that.data.goodsList;
    var couponInfo = that.data.couponInfo;
    var duihuanIndex = that.data.duihuanIndex;
    var shopInfo = that.data.shopInfo;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_community_coupon_change',
        cid: couponInfo.id
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          app.errorTip(that, res.data.data.msg, 2000);
          goodsList[duihuanIndex].remainCount = res.data.data.remainCount;
          goodsList[duihuanIndex].isExchange = res.data.data.isExchange;
          shopInfo.points = res.data.data.points;
          that.setData({
            goodsList: goodsList,
            shopInfo: shopInfo
          })
          that.hideCoupon();
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      curShow: 'good'
    });
    that.requestShopinfo();
    that.requestGoodslist();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      var type = that.data.curShow;
      if (type == 'good') {
        that.requestGoodslist();
      } else if (type == 'coupon') {
        that.requestCouponlist();
      }
    }
  },
  togoodDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages/jfGoodDetail/jfGoodDetail?id=' + id
    })
  },
  tointegralDetails: function () {
    wx.navigateTo({
      url: '/subpages/integralDetails/integralDetails'
    })
  },
  tointegralRules: function () {
    wx.navigateTo({
      url: '/subpages/integralRules/integralRules'
    })
  },
  toLottery: function () {
    var that = this;
    var isStart = that.data.shopInfo.lottery.start;
    if (isStart) {
      wx.navigateTo({
        url: '/subpages/getReward/getReward',
      })
    } else {
      var msg = that.data.shopInfo.lottery.msg;
      app.errorTip(that, msg, 2000);
    }
  },
  toexchangeDetail: function () {
    wx.navigateTo({
      url: '/subpages/exchangeDetail/exchangeDetail',
    })
  },
  // 兑换会员
  toexchangeVip: function () {
    wx.navigateTo({
      url: '/subpages0/exchangeVip/exchangeVip',
    })
  },
  // 兑换余额
  toexchangeYe: function () {
    wx.navigateTo({
      url: '/subpages0/exchangeYe/exchangeYe',
    })
  },
  onShareAppMessage: function () {
    var that = this;
    var title = '积分商城';
    // console.log(title);
    // var shareInfo = app.globalData.shareInfo;
    // title = shareInfo.shareTitle ? shareInfo.shareTitle : title;
    // var cover = shareInfo.shareCover ? shareInfo.shareCover : '';
    app.getPoint(that);
    return {
      title: title,
      // imageUrl: cover,
      path: '/subpages/integralShop/integralShop?enterfrom=share'
    }
  },
})